<template>
    <!-- 列表页面 -->
    <div class="tableSample">
        <div class="header">
            <div class="header-left">
                <p class="title">用户余额记录
                    <el-button type="primary" plain style="margin-left: 30px;" @click="exportToExcel" :loading="excelLoad">导出信息</el-button>
                </p>
            </div>
            <div class="header-right">
                <lin-search @query="onQueryChangeTwo" placeholder="请输入会员等级"/>
                <lin-search @query="onQueryChange" placeholder="请输入会员卡号" style="margin-left: 10px;"/>
                <lin-search @query="onQueryChangeOne" placeholder="请输入昵称" style="margin-left: 10px;"/>
                <el-dropdown  size="medium" style="margin: 0 10px;" @command="handleCommand" v-auth="'查询'">
                    <el-button  size="medium">
                        {{searchType ? searchType : '类型'}}
                        <i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="['类型']">类型</el-dropdown-item>
                        <el-dropdown-item
                                v-for="type in typeArr"
                                :key="type.id"
                                :command="[type]">{{type.name}}
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
                <lin-date-picker @dateChange="handleDateChange" ref="searchDate" class="date">
                </lin-date-picker>
            </div>
        </div>
        <!-- 表格 :default-expand-all="true"-->
        <el-table
                :data="tableData"
                stripe
                border
                v-loading="loading"
        >
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="user.user_no"
                    label="会员卡号">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="user.grade_name"
                    label="会员等级">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="user.nickname"
                    label="用户昵称">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="money"
                    label="金额">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="type_name"
                    label="类型">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="user_money"
                    label="用户原有金额">
            </el-table-column>
            <el-table-column
                    :show-overflow-tooltip="true"
                    prop="create_time"
                    label="时间">
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pagination" v-if="total_nums > 0">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :background="true"
                    :page-size="pageCount"
                    :current-page="currentPage"
                    v-if="refreshPagination"
                    layout="total,prev, pager, next, jumper"
                    :total="total_nums"
            ></el-pagination>
        </div>
    </div>
</template>

<script>
import finance from '@/models/finance'
import LinSearch from '@/components/base/search/lin-search'
import LinDatePicker from '@/components/base/date-picker/lin-date-picker'

export default {
  components: {
    LinSearch,
    LinDatePicker,
  },
  data() {
    return {
      tableData: [],
      operate: [],
      loading: false,
      excelLoad: false,
      searchKeyword: '',
      // 分页相关
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      currentPage: 1, // 默认获取第一页的数据
      pageCount: 30, // 每页10条数据
      total_nums: 0, // 分组内的总数
      search: {
        grade_name: '',
        user_no: '',
        nickname: '',
        type: '',
        start_date: '',
        end_date: '',
      },
      typeArr: [
        { id: 1, name: '余额充值得到' },
        { id: 2, name: '支付订单失去' },
        { id: 3, name: '直接使用失去' },
        { id: 4, name: '后台操作得到' },
        { id: 5, name: '后台操作失去' },
        { id: 6, name: '公众号余额得到' },
        { id: 7, name: '商城退款得到' },
      ],
      searchType: '',
      searchDate: ['', ''],
    }
  },
  computed: {},
  created() {
    this.loading = true
    // 获取数据
    this._getTableData(this.currentPage, this.pageCount)
  },
  watch: {
    // 日期搜索
    searchDate(newDate) {
      if (newDate && newDate.length) {
        this.searchDate = newDate
      } else {
        this.searchDate = ['', '']
      }
      this.loading = true
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
  },
  methods: {
    async exportToExcel() {
      const self = this
      self.excelLoad = true
      const res = await finance.getUserMoneyExcelList({
        grade_name: this.search.grade_name,
        user_no: this.search.user_no,
        nickname: this.search.nickname,
        type: this.search.type,
        start_date: this.searchDate[0],
        end_date: this.searchDate[1],
      })
      if (res.length === 0) {
        self.excelLoad = false
        return false
      }
      const list = []
      res.forEach((item) => {
        list.push({
          user_no: item.user.user_no,
          grade_name: item.user.grade_name,
          nickname: item.user.nickname,
          money: item.money,
          type_name: item.type_name,
          user_money: item.user_money,
          create_time: item.create_time,
        })
      })
      require.ensure([], () => {
        const {
          export_json_to_excel,
          // eslint-disable-next-line global-require
        } = require('../excel/Export2Excel')
        const tHeader = [
          '会员卡号',
          '会员等级',
          '用户昵称',
          '金额',
          '类型',
          '用户原有余额',
          '时间',
        ]
        const filterVal = [
          'user_no',
          'grade_name',
          'nickname',
          'money',
          'type_name',
          'user_money',
          'create_time',
        ]
        const data = self.formatJson(filterVal, list)
        const date = new Date()
        const year = date.getFullYear()
        const month = date.getMonth() + 1// js中是从0开始所以要加1
        const day = date.getDate()
        export_json_to_excel(tHeader, data, `用户余额记录${year}${month}${day}`)
      })
      self.excelLoad = false
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    // 获取数据
    async _getTableData(start, count) {
      const res = await finance.getUserMoneyList(start, count, {
        grade_name: this.search.grade_name,
        nickname: this.search.nickname,
        user_no: this.search.user_no,
        type: this.search.type,
        start_date: this.searchDate[0],
        end_date: this.searchDate[1],
      })
      this.tableData = res.data
      this.total_nums = res.count
      this.$nextTick(() => {
        this.loading = false
      })
    },
    rowClick(val) {
      console.log(val)
    },
    // 切换分页
    async handleCurrentChange(val) {
      this.currentPage = val
      this.loading = true
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索
    onQueryChange(query) {
      this.loading = true
      this.search.user_no = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.currentPage = 1
      this.pageCount = 30
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索昵称
    onQueryChangeOne(query) {
      this.loading = true
      this.search.nickname = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 会员等级搜索
    onQueryChangeTwo(query) {
      this.loading = true
      this.search.grade_name = query.trim()
      if (!query) {
        this._getTableData(1, 10)
        return
      }
      this.currentPage = 1
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 搜索状态
    handleCommand(status) {
      this.searchType = status[0]['name'] // eslint-disable-line
      this.currentPage = 1
      this.loading = true
      // eslint-disable-next-line eqeqeq
      if (status[0] != '类型') {
        this.search.type = status[0].id
      } else {
        this.search.type = ''
      }
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
    // 日期搜索
    handleDateChange(date) {
      this.searchDate = date
      this.currentPage = 1
      this.loading = true
      this._getTableData(
        this.currentPage,
        this.pageCount,
      )
    },
  },
}
</script>

<style lang="scss" scoped>
    .tableSample {
        padding: 0 0 30px;

        .header {
            display: flex;
            width: 100%;
            overflow-x:scroll;
            justify-content: space-between;
            align-items: center;
            padding: 0 30px;
            border-bottom: 1px solid #dae1ed;

            .header-left {
                float: left;
                min-width: 270px;
                .title {
                    height: 59px;
                    line-height: 59px;
                    color: $parent-title-color;
                    font-size: 16px;
                    font-weight: 500;
                }
            }

            .header-right {
                float: right;
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-right: 30px;
            }
        }

        .table-main {
            padding: 0 30px;
            margin-top: 30px;
        }

        .top-operate {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            margin-top: 30px;
        }

        .sort-input {
            width: 50px;
            background: none;
            justify-content: space-around;
        }

        .table-edit {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            .content {
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .cell-icon {
                cursor: pointer;
                color: #3963bc;
                font-size: 16px;
            }

            .cell-icon-edit {
                display: flex;
                margin-left: 20px;
                width: 50px;
                justify-content: space-between;

                .cell-cancel {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                }

                .cell-save {
                    cursor: pointer;
                    color: #3963bc;
                    font-size: 16px;
                    margin-right: -20px;
                }
            }
        }

        .m-20 {
            margin-bottom: 10px;
            margin-top: 5px;
        }

        .summary {
            display: flex;
            justify-content: flex-start;
            flex-direction: row;

            img {
                width: 135px;
                height: 200px;
            }

            .demo-table-expand {
                font-size: 0;
                margin-left: 30px;
                display: flex;
                flex-direction: column;

                label {
                    width: 90px;
                    color: #99a9bf;
                }

                .el-form-item {
                    margin-right: 0;
                    margin-bottom: 0;
                    display: flex;
                    flex-direction: row;
                }
            }
        }

        .pagination {
            display: flex;
            justify-content: flex-end;
            margin: 20px;
        }
    }
    // dialog
    .tableSample /deep/ .el-dialog__footer {
        text-align: left;
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__header {
        padding-left: 30px;
    }

    .tableSample /deep/ .el-dialog__body {
        padding: 30px;
    }
</style>
